package crons;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskOptions.Method;

import models.ApplicationModel;

import entities.Application;

@SuppressWarnings("serial")
public class CronAutoSite extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(CronAutoSite.class.getName());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationModel appModel = ApplicationModel.getInstance();
		List<Application> apps = appModel.getAll();
		
		for (Application app : apps) {
			Queue queue = QueueFactory.getDefaultQueue();
			String queueUrl = "/cron-lastest-article/" + app.getId();
			queue.add(TaskOptions.Builder.withUrl(queueUrl).method(Method.GET));
			log.info(queueUrl);
			response.getWriter().println("Cron autoSite id: " + app.getId());
		}
		
	}
}
